package com.kantboot.business.water.web.contrller;

import cn.hutool.extra.qrcode.QrConfig;
import com.kantboot.business.water.domain.dto.BusWaterGoodsOrderSearchDTO;
import com.kantboot.business.water.domain.dto.BusWaterGoodsOrderSearchOfManageDTO;
import com.kantboot.business.water.domain.dto.GoodsGenPayOrderDTO;
import com.kantboot.business.water.domain.vo.OrderMoneyVO;
import com.kantboot.business.water.service.IBusWaterGoodsOrderService;
import com.kantboot.functional.pay.order.domian.entity.FunctionalPayOrder;
import com.kantboot.util.common.result.RestResult;
import com.kantboot.util.core.param.PageParam;
import com.kantboot.util.core.result.PageResult;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import java.awt.*;

/**
 * 提水点
 */
@RestController
@RequestMapping("/bus/water/order")
public class BusWaterOrderController {

    @Resource
    private IBusWaterGoodsOrderService service;


    @RequestMapping("/getBodyDataFront")
    public RestResult<PageResult> getBodyDataFront(@RequestBody PageParam<BusWaterGoodsOrderSearchDTO> page) {
        return RestResult.success(service.getBodyDataFront(page), "getSuccess", "获取成功");
    }

    @RequestMapping("/getBodyDataFrontSelf")
    public RestResult<PageResult> getBodyDataFrontSelf(@RequestBody PageParam<BusWaterGoodsOrderSearchDTO> page) {
        return RestResult.success(service.getBodyDataFrontSelf(page), "getSuccess", "获取成功");
    }

    @RequestMapping("/generatePayOrder")
    public RestResult<FunctionalPayOrder> generatePayOrder(@RequestBody GoodsGenPayOrderDTO dto) {
        return RestResult.success(service.generatePayOrder(dto), "getSuccess", "获取成功");
    }

    @RequestMapping("/getDiscountPriceBySelfAndGoodsId")
    public RestResult<OrderMoneyVO> getDiscountPriceBySelfAndGoodsId(
            @RequestParam("goodsId") Long goodsId,
            @RequestParam("goodsNumber") Long goodsNumber
    ) {
        return RestResult.success(service.getDiscountPriceBySelfAndGoodsIdAndGoodsNumber(goodsId, goodsNumber), "getSuccess", "获取成功");
    }

    @RequestMapping("/getCountOfGoodsOrderSelf")
    public RestResult getCountOfGoodsOrderSelf() {
        return RestResult.success(service.getCountOfGoodsOrderSelf(), "getSuccess", "获取成功");
    }

    @RequestMapping("/orderToNotProvided")
    public RestResult orderToNotProvided(@RequestParam("id") Long id) {
        service.orderToNotProvided(id);
        return RestResult.success(Boolean.TRUE, "optSuccess", "操作成功");
    }

    @RequestMapping("/getBodyDataFrontSelfManager")
    public RestResult<PageResult> getBodyDataFrontSelfManager(@RequestBody PageParam<BusWaterGoodsOrderSearchOfManageDTO> page) {
        return RestResult.success(service.getBodyDataFrontSelfManager(page), "getSuccess", "获取成功");
    }

    /**
     * 生成订单提货UUID
     */
    @RequestMapping("/generatePickUuid")
    public RestResult generatePickUuid(@RequestParam("id") Long id) {
        return RestResult.success(service.generatePickUuid(id), "getSuccess", "获取成功");
    }

    /**
     * 根据uuid核销商品
     * @param uuid uuid
     * @return 核销结果
     */
    @RequestMapping("/pickGoodsByUuid")
    public RestResult pickGoodsByUuid(@RequestParam("uuid") String uuid) {
        service.pickGoodsByUuid(uuid);
        return RestResult.success(Boolean.TRUE, "pickSuccess", "核销成功");
    }

    @Resource
    private HttpServletResponse response;

    /**
     * 根据uuid生成二维码图片流
     */
    @SneakyThrows
    @RequestMapping("/generatePickUuidQrCode/{uuid}")
    public void generatePickUuidQrCode(@PathVariable("uuid") String uuid) {
        QrConfig config = new QrConfig(300, 300);
        // 设置边距，即二维码和背景之间的边距
        config.setMargin(3);
        // 设置前景色，即二维码颜色（白色）
        config.setForeColor(Color.WHITE.getRGB());
        // 设置背景色（黑色）
        config.setBackColor(Color.BLACK.getRGB());
        // 生成二维码到流
        cn.hutool.extra.qrcode.QrCodeUtil.generate(uuid, config, "png", response.getOutputStream());
    }


    @RequestMapping("/getByUuid")
    public RestResult getByUuid(@RequestParam("uuid") String uuid) {
        return RestResult.success(service.getByUuid(uuid), "getSuccess", "获取成功");
    }

}
